//
//  ImageListViewController.m
//  HuiWenMovie
//
//  Created by ios on 15/7/22.
//  Copyright (c) 2015年 ios. All rights reserved.
//

#import "ImageListViewController.h"
#import "UIImageView+WebCache.h"
#import "DataService.h"
#import "ImageListCollectionViewCell.h"
#import "Photo Viewer.h"
#import "BaseNavigationController.h"
#define kwth [UIScreen mainScreen].bounds.size.width
#define kheight [UIScreen mainScreen].bounds.size.height



@interface ImageListViewController ()
{
    UIImageView *imageView;
}

@end

@implementation ImageListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"厨子戏子痞子";
    [self loadImages];
    [self configureImageListCollectionView];
    // Do any additional setup after loading the view.
}

- (NSArray *)content
{
    
    if(_content ==nil)
    {
        _content = [[NSArray alloc]init];
    }

    return _content;
}

- (void)configureImageListCollectionView
{
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
    layout.itemSize = CGSizeMake((kwth - 50)/4, 80);
    layout.minimumInteritemSpacing = 10;
    layout.minimumLineSpacing = 10;
    
    _ImageList = [[UICollectionView alloc]initWithFrame:self.view.bounds collectionViewLayout:layout];
    _ImageList.dataSource = self;
    _ImageList.delegate = self;
    [_ImageList registerClass:[ImageListCollectionViewCell class] forCellWithReuseIdentifier:@"ImageListCell"];
    
    [self.view addSubview:_ImageList];

}

- (void)loadImages
{
    _content = [DataService getJsonDataFromFile:@"image_list.json"];
    
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return [_content count];
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    ImageListCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"ImageListCell" forIndexPath:indexPath];
    
    NSDictionary *dic = _content[indexPath.row];
    ImageListModel *model = [[ImageListModel alloc]init];
    [model setValuesForKeysWithDictionary:dic];
       
    cell.imageListModel = model;
    
    
    return cell;

}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    Photo_Viewer *photoViewController = [[Photo_Viewer alloc]init];
    BaseNavigationController *navForPhotoViewer = [[BaseNavigationController alloc]initWithRootViewController:photoViewController];
    NSMutableArray *imageStringOfUrl = [[NSMutableArray alloc]init];
    for (NSDictionary *dic in _content)
    {
        NSString *temp = [dic objectForKey:@"image"];
        [imageStringOfUrl addObject:temp];
    }
    photoViewController.currentIndex = indexPath.row;
    photoViewController.imageUrl = imageStringOfUrl;

    [self presentViewController:navForPhotoViewer animated:YES completion:nil];
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
